package com.globallogic.vkulyk.DayOfTheWeekFinder;

import java.util.*;

/**
 * A program that prints out the day of the week for a given day (1..31), month
 * (1..12) and year. For example, for a given input "10 October 2010" it should
 * print to the screen "Sunday" ??month is string or int??
 * 
 * @author Viacheslav Kulyk
 * @version 1.0 20 Jan 2013
 */

public class DayOfTheWeekFinder {

	public static final int NUMBER_OF_DAYS_IN_NON_LEAP_YEAR = 365;
	public static final int NUMBER_OF_DAYS_IN_LEAP_YEAR = 366;
	public static final int LEAP_YEAR_PERIOD_SHORT = 4;
	public static final int LEAP_YEAR_PERIOD_LONG = 400;
	public static final int NON_LEAP_YEAR_PERIOD = 100;
	public static final int WEEK_DAYS_NUMBER = 7;
	private int date = 0;
	private int month = 0;
	private int year = 0;
	private String dayOfWeekName = "";

	public static void main(String[] args) {
		new DayOfTheWeekFinder().getDayOfWeekByDate();
	}

	public void getDayOfWeekByDate() {
		getDate();
		calculateWeekDayName();
		printWeekDay();
	}

	private void getDate() {
		Scanner scanner = new Scanner(System.in);
		System.out
				.print("Enter the date in format 'date(1..31) month(1..12) year', e.g. '9 5 2013', and press 'Enter' key:");
		date = scanner.nextInt();
		month = scanner.nextInt();
		year = scanner.nextInt();

	}

	private void calculateWeekDayName() {
		int numOfDaysSinceDay1 = getNumOfDaysInYears()
				+ getNumOfDaysInMonths() + date - 1;
		int dayOfWeekDeltaSinceDay1 = numOfDaysSinceDay1
				% DayOfTheWeekFinder.WEEK_DAYS_NUMBER;
		dayOfWeekName = getDayOfWeekName(dayOfWeekDeltaSinceDay1);
	}

	private int getNumOfDaysInYears() {
		int result = 0;
		for (int i = 1; i < year; i++) {
			if (isLeapYear()) {
				result = result
						+ DayOfTheWeekFinder.NUMBER_OF_DAYS_IN_LEAP_YEAR;
			} else {
				result = result
						+ DayOfTheWeekFinder.NUMBER_OF_DAYS_IN_NON_LEAP_YEAR;
			}
		}
		return result;
	}

	private boolean isLeapYear() {
		return ((year % DayOfTheWeekFinder.LEAP_YEAR_PERIOD_SHORT == 0)
				&& (year % DayOfTheWeekFinder.NON_LEAP_YEAR_PERIOD != 0) || (year
				% DayOfTheWeekFinder.LEAP_YEAR_PERIOD_LONG == 0));
	}

	private int getNumOfDaysInMonths() {
		int result = 0;
		int daysInFebr = 0;
		if (isLeapYear()) {
			daysInFebr = 29;
		} else {
			daysInFebr = 28;
		}
		int[] days = { 31, daysInFebr, 31, 30, 31, 30, 31, 31, 30, 30, 30, 31 };
		for (int i = 1; i < month; i++) {
			result = result + days[i - 1];
		}
		return result;
	}

	private String getDayOfWeekName(int dayNum) {
		String result = "";
		String[] names = { "Saturday", "Sunday", "Monday", "Tuesday",
				"Wednesday", "Thursday", "Friday" };
		result = names[dayNum];
		return result;
	}

	private void printWeekDay() {
		System.out.print(dayOfWeekName);
	}

}
